package com.example.prompt.service.text;

import com.alibaba.fastjson.TypeReference;
import com.example.prompt.dto.PromptDto;
import com.example.prompt.service.PromptCommon;
import com.example.prompt.utils.JsonUtil;
import com.feeyo.llm.api.chat.Chat;
import com.feeyo.llm.api.chat.ChatFactory;
import com.feeyo.llm.api.chat.Choice;
import com.feeyo.llm.api.chat.Message;
import com.feeyo.llm.api.chat.feeyo.QwenResponse;
import com.feeyo.llm.api.chat.moonshot.MoonshotResponse;
import com.feeyo.llm.api.chat.openai.ChatGPTResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * 用于文本总结
 */

@Service
@Slf4j
@RequiredArgsConstructor
public class TextSummaryService {

    @Value("${file.upload.dir}")
    private String UPLOAD_DIR;

    @Value("${ai.model.api-key}")
    private String apiKey;

    /**
     * 用于文本总结
     *
     * @param text
     */
    public void textSummary(PromptDto text) {
        log.info("textSummary: {}", text);
        String fileName = text.getFileId();
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        if (!StringUtils.equals(fileExtension, ".txt")) {
            log.info("只处理txt文本");
            return;
        }
        File file = new File(UPLOAD_DIR + fileName);
        StringBuffer stringBuffer = PromptCommon.textSummaryPrompt();
    }


    public void textToImage(PromptDto request) {
        int x = 32;
        String model = "deepseek/deepseek-reasoner";
        String apiKey = "YWMwMDF3YW55";
        String s = interactiveChat(x, model, apiKey, request.getFileType());
        log.warn("----------");
        log.warn(s);

    }

    private String interactiveChat(int x, String model, String currentApiKey, String prompt) {
        int length = prompt.length();
        try {
            if(StringUtils.isEmpty(currentApiKey)){
                currentApiKey = apiKey;
            }
            Chat chat = ChatFactory.newChat(x, currentApiKey);
            String responseTxt = chat.ask(model, prompt);
            log.info(responseTxt);
            switch(x) {
                case ChatFactory.OPENAI:
                case ChatFactory.MS_AZURE:
                case ChatFactory.CDN35: {
                    ChatGPTResponse<Message> response = JsonUtil.unmarshalFromString(responseTxt,
                            new TypeReference<ChatGPTResponse<Message>>() {});
                    final Choice<Message> choice = response.getChoices().iterator().next();
                    return choice.getMessage().getContent();
                }
                case ChatFactory.MOONSHOT: {
                    MoonshotResponse<Message> response = JsonUtil.unmarshalFromString(responseTxt,
                            new TypeReference<MoonshotResponse<Message>>() {});
                    final Choice<Message> choice = response.getChoices().iterator().next();
                    return choice.getMessage().getContent();
                }
                case ChatFactory.QWen_7B:
                case ChatFactory.DeepSeek: {
                    QwenResponse<Message> response = JsonUtil.unmarshalFromString(responseTxt,
                            new TypeReference<QwenResponse<Message>>() {});
                    final Choice<Message> choice = response.getChoices().iterator().next();
                    return choice.getMessage().getContent();
                }


            }
        } catch (Throwable e) {
            log.warn("chat err: {}, {}", length, model, e);
        }
        return null;
    }


}
